﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using System.IO;
using Yugi.Client.Card.FileDecoder;

namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for Deck_Container.xaml
	/// </summary>
	public partial class Store_Container : UserControl
	{
        public Store_Container()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(Store_Container_Loaded);
		}

        void Store_Container_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                layout = LayoutManager.LayoutManager.MainWindow.Content as DeckConstructLayout;
                //conteiner.Children.Clear();
                //Test();
            }
            catch (Exception ex)
            {
            }
        }

        void img_MouseEnter(object sender, MouseEventArgs e)
        {
           
            YBaseCard card = ((Image)sender).DataContext as YBaseCard;
            layout.GetCardImageDetail().Source = card.displayImage;
            layout.GetCardTextDetail().SetText(card.Description);
        }

        void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
          
            YBaseCard card = ((Image)sender).DataContext as YBaseCard;
            layout.GetDeckContainer().Add(card);
            

        }

        //Load 18 Blank Image into Container
        private void PreLoad()
        {
           
            for(int i=0;i<18;i++)
            {
                Image img = new Image();
                img.Width = 60;
                img.Height = 80;
                if(i %9==0)
                {
                    img.Margin= new Thickness(5,10,0,0);
                }
                else
                {
                    img.Margin = new Thickness(10, 10, 0, 0);
                }
                conteiner.Children.Add(img);
                img.MouseEnter += new MouseEventHandler(img_MouseEnter);
                img.MouseDown += new MouseButtonEventHandler(img_MouseDown);
            }
        }

      

        // Load Current Card
        private void Load()
        {
            if(conteiner.Children.Count!=18)
            {
                PreLoad();
            }

            // Update Image
            int d=0;
            int max=Math.Min(CurrentPage * 18+18,allcards.Count);
            int i;
            for (i= CurrentPage * 18; i < max; i++)
            {
                if (!(conteiner.Children[d] is Image)) d++;
                ((Image)conteiner.Children[d]).Source = allcards[i].displayImage;
                ((Image)conteiner.Children[d]).DataContext = allcards[i];
                d++;
            }

            // Blank Left Image
            if (d < 18)
            {
                for (int j = d; j < 18; j++)
                {
                    ((Image)conteiner.Children[j]).Source = null;
                    ((Image)conteiner.Children[j]).DataContext = null;
                }
            }
        }

        // Next Page
        public void Next()
        {
            if (CurrentPage < MaxPage)
            {
                CurrentPage++;
                Load();
            }
            this.label_txt.Text = String.Format(" {0} / {1} ", CurrentPage + 1, MaxPage + 1);
        }

        // Previous Page
        public void Back()
        {
            if (CurrentPage > 0)
            {
                CurrentPage--;
                Load();
            }
            this.label_txt.Text = String.Format(" {0} / {1} ", CurrentPage + 1, MaxPage + 1);
        }

        DeckConstructLayout layout;
        private List<YBaseCard> allcards = new List<YBaseCard>();
        public int CurrentPage { get; private set; }
        public int MaxPage { get; private set; }
        public List<YBaseCard> AllCards
        {
            get
            {
                return allcards;
            }
            set
            {
                allcards = value;
                CurrentPage = 0;
                MaxPage = allcards.Count / 18;
                Load();
                this.label_txt.Text = String.Format(" {0} / {1} ", CurrentPage + 1, MaxPage + 1);
            }
        }

        private void OnClickNext(object sender, RoutedEventArgs e)
        {
            Next();
        }

        private void OnClickPre(object sender, RoutedEventArgs e)
        {
            Back();
        }

        public void InitializeCard(List<int> data,List<int> curren_deck)
        {
            if (this.layout==null)
                layout = LayoutManager.LayoutManager.MainWindow.Content as DeckConstructLayout;
            this.conteiner.Children.Clear();
            List<YBaseCard> db = Test();
            List<YBaseCard> current = new List<YBaseCard>();
            YBaseCard card;
            foreach (int item in data)
            {
                card= db.Find(P => P.Card_TypeID == item);
                if(!current.Contains(card))
                    current.Add(card);
            }

            this.AllCards = current;
            
            //Init Deck
            foreach (var item in curren_deck)
            {
                card = db.Find(P => P.Card_TypeID == item);
                if (card != null)
                    layout.GetDeckContainer().Add(card);
            }
            db = null;
        }

        //--------------------------------Test------------------------------------//
        private List<YBaseCard> Test()
        {
            List<YBaseCard> test = new List<YBaseCard>();
            YFileDecoder decoder = new YFileDecoder();
            //StreamWriter sw = new StreamWriter("sumary.txt");
            
            foreach (var item in Directory.GetFiles(Directory.GetCurrentDirectory()+"\\player"))
            {
                YBaseCard card = decoder.BinaryFileDecoderCard(item);
                FileInfo f = new FileInfo(item);
                card.Card_TypeID = Int32.Parse(f.Name.Split('.')[0].Split('_')[1]);
                card.ID = card.Card_TypeID;
                test.Add(card);
               // sw.WriteLine("{0}:{1}", card.Card_TypeID, card.Name);
            }
            //sw.Close();
            return test;
        }
	}
}